use super::*; 

pub struct Sequence {
    ptr: usize,
    finished: bool,
    children: Vec<Box<dyn ITask>>,
}

impl Sequence {
    pub fn new<It>(children: It) -> Box<Self> where It: IntoIterator<Item=Box<dyn ITask>> {
        Box::new(Self {
            ptr: 0,
            finished: false,
            children: children.into_iter().collect(),
        })
    }

    pub fn add_child(&mut self, child: Box<dyn ITask>) {
        self.children.push(child);
    }
}

impl ITask for Sequence {
    fn step(&mut self) {
        self.children[self.ptr].step();

        if self.children[self.ptr].is_finished() {
            self.ptr += 1;
        }

        if self.ptr >= self.children.len() {
            self.finished = true;
        }
    }

    fn reset(&mut self) {
        self.ptr = 0;
        for child in self.children.iter_mut() {
            child.reset();
        }
    }

    fn is_finished(&self) -> bool {
        self.finished
    }
}
